package com.syy.spider.controller;

import com.syy.spider.dao.CommentMapper;
import com.syy.spider.dao.UserMapper;
import com.syy.spider.pojo.Comment;
import com.syy.spider.pojo.User;
import com.syy.spider.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Http;
import java.util.List;
import java.util.Map;

@Controller
public class CommentController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private CommentService commentService;

    @RequestMapping("/comment")
    public String toComment(){
        return "pages/comment";
    }

    @RequestMapping("/addComment")
    @ResponseBody
    public int addComment(HttpServletRequest request, Http , @RequestBody Map<String, Object> params){
        User user = (User) request.getSession().getAttribute("user");
        User user1 = userMapper.selectUserByName(user.getUsername());
        int a;
        if (user1.getCom_aut()==1){
            String comment = (String) params.get("comment");
            String username = user.getUsername();
            a = commentService.addComment(comment,username);
            System.out.println(a);
        }else {
            a = -1;
        }
        return a;
    }

    @RequestMapping("/getComments")
    @ResponseBody
    public List<Comment> getComments(){
        return commentMapper.getComments();
    }

    @RequestMapping("/getResponses")
    @ResponseBody
    public List<Comment> getResponses(@RequestBody Map<String, Object> params){
        int id = (int) params.get("id");
        return commentMapper.getResponses(id);
    }

    @RequestMapping("/addResopnse")
    @ResponseBody
    public int addResopnse(HttpServletRequest request, HttpSession session, @RequestBody Map<String, Object> params){
        User user = (User) request.getSession().getAttribute("user");
        User user2 = userMapper.selectUserByName(user.getUsername());
        int a;
        if (user2.getCom_aut()==1){
            String comment = (String) params.get("comment");
            int id = (int) params.get("id");
            Comment comment1 = commentMapper.selectCommentById(id);
            String user1 = comment1.getUser();
            String username = user.getUsername();
            a = commentMapper.addRes(comment,username,id,user1);
            System.out.println(a);
        }else {
            a = -1;
        }
        return a;
    }

    @RequestMapping("/jubao")
    @ResponseBody
    public int jubao(@RequestBody Map<String, Object> params){
        int id = (int) params.get("id");
        Comment comment = commentMapper.selectCommentById(id);
        String username = comment.getUser();
        User user = userMapper.selectUserByName(username);
        Integer id1 = user.getId();
        Integer jubao = user.getJubao();
        jubao = jubao + 1;
        int a = userMapper.updateByJuBao(id1, jubao);
        System.out.println(a);
        return a;
    }
}
